package GUI;


import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.Font;
import java.awt.Color;
import javax.swing.JComboBox;

import Entity.*;

import net.sourceforge.jdatepicker.impl.JDatePanelImpl;
import net.sourceforge.jdatepicker.impl.JDatePickerImpl;
import net.sourceforge.jdatepicker.impl.SqlDateModel;

public class CoordinatingMeeting extends JPanel{

	
	private static final long serialVersionUID = 1L;
	private JDatePickerImpl mdatePicker;
	private JTextField mTimetTxtField;
	private JButton btnBack;
	private JButton btnCoordinate;
	private JLabel iconlabel;
	private JComboBox<String> ManagerInchargecomboBox;
	private JComboBox<String> cIDcomboBox;
	private JDatePickerImpl edatePicker;
	
	private ArrayList<Meeting> m_allData;
	private ArrayList<String> all_managets;
	private String filterall_managets;
	private ArrayList<Meeting> m_filterallallData;
	private Object syncObject;
	private boolean m_isChangeAllowed=true;
	
	private JLabel lblGroomBride;
	private JComboBox<String> gbNamescomboBox;
	private JLabel lblPlace;
	private JTextField PlacecomboBox;
	private JButton btnRefresh;
	/**
	 *Constructor 
	 */
	public CoordinatingMeeting() {
		setLayout(null);
		mTimetTxtField = new JTextField();
		mTimetTxtField.setFont(new Font("Tahoma", Font.BOLD, 12));
		mTimetTxtField.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent arg0) {
				mTimetTxtField.setText("");
				mTimetTxtField.setEnabled(true);
			}
		});
		gbNamescomboBox = new JComboBox<String>();
		gbNamescomboBox.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				synchronized (syncObject) {
					if(isChangeAllowed())
							filterList(gbNamescomboBox.getSelectedItem().toString(),"G_B_NAME");
				}
			}
		});
		mTimetTxtField.setText("hh:mm:ss");
		mTimetTxtField.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				synchronized (syncObject) {
					if(isChangeAllowed())
						if(mTimetTxtField.getText().toString().length()==8&&mTimetTxtField.getText().toString().contains(":"))
							{
							if(!(getMeetingDate().equals("") && mTimetTxtField.getText().toString().equals("")))
								filtermanager(getMeetingDate(),mTimetTxtField.getText().toString());
							}
						else
							JOptionPane.showMessageDialog(null, "Time must be in fornat hh:mm:ss", "", JOptionPane.INFORMATION_MESSAGE);
					
				}
			}
		});
		btnBack = new JButton("Back");
		btnBack.setIcon(new ImageIcon(CoordinatingMeeting.class.getResource("/resources/back22.png")));
		btnCoordinate = new JButton("coordinate");
		btnCoordinate.setIcon(new ImageIcon(CoordinatingMeeting.class.getResource("/resources/hp_notepad_pencil.png")));
		ManagerInchargecomboBox = new JComboBox<String>();
		PlacecomboBox = new JTextField();

		btnCoordinate.setFont(new Font("Tahoma", Font.BOLD | Font.ITALIC, 12));
		btnRefresh = new JButton("Refresh");
		
		initialize();
	}

	private void initialize() {
		
		JLabel lblCoordinatingMeeting = new JLabel("Coordinating Meeting");
		lblCoordinatingMeeting.setForeground(new Color(0, 204, 255));
		lblCoordinatingMeeting.setFont(new Font("Tahoma", Font.BOLD, 16));
		lblCoordinatingMeeting.setBounds(10, 11, 202, 27);
		add(lblCoordinatingMeeting);
		
		JLabel lblCustomerId = new JLabel("Customer ID");
		lblCustomerId.setForeground(Color.WHITE);
		lblCustomerId.setFont(new Font("Tahoma", Font.BOLD, 12));
		lblCustomerId.setBounds(10, 187, 86, 27);
		add(lblCustomerId);
		
		JLabel lblManagerincharge = new JLabel("Manager Incharge");
		lblManagerincharge.setForeground(Color.WHITE);
		lblManagerincharge.setFont(new Font("Tahoma", Font.BOLD, 12));
		lblManagerincharge.setBounds(372, 280, 126, 27);
		add(lblManagerincharge);
		
		JLabel lblMeetingDate = new JLabel("Meeting Date");
		lblMeetingDate.setForeground(Color.WHITE);
		lblMeetingDate.setFont(new Font("Tahoma", Font.BOLD, 12));
		lblMeetingDate.setBounds(372, 102, 112, 27);
		add(lblMeetingDate);
		
		JLabel lblMeetingTime = new JLabel("Meeting Time");
		lblMeetingTime.setForeground(Color.WHITE);
		lblMeetingTime.setFont(new Font("Tahoma", Font.BOLD, 12));
		lblMeetingTime.setBounds(372, 187, 112, 27);
		add(lblMeetingTime);
		
		mTimetTxtField.setBounds(518, 188, 137, 27);
		add(mTimetTxtField);
		mTimetTxtField.setColumns(10);
		
		btnBack.setFont(new Font("Tahoma", Font.BOLD | Font.ITALIC, 12));
		btnBack.setBounds(10, 462, 101, 27);
		add(btnBack);
		
		btnCoordinate.setBounds(443, 446, 135, 27);
		add(btnCoordinate);
		
		ImageIcon LOGOImageIcon = new ImageIcon(getClass().getClassLoader().getResource("resources/camera-background3.png"));
		ImageIcon LOGO = new ImageIcon (LOGOImageIcon.getImage().getScaledInstance(-1, -1, java.awt.Image.SCALE_SMOOTH));
		
		
		
		ManagerInchargecomboBox.setBounds(518, 281, 137, 27);
		AutoCompletion.enable(this.ManagerInchargecomboBox);
		add(ManagerInchargecomboBox);
		
		SqlDateModel model = new SqlDateModel();
		JDatePanelImpl datePanel = new JDatePanelImpl(model);
		mdatePicker = new JDatePickerImpl(datePanel);
		mdatePicker.getJFormattedTextField().addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				synchronized (syncObject) {
					if(isChangeAllowed())
						if(!(getMeetingDate().equals("") && mTimetTxtField.getText().toString().equals("")))
							filtermanager(getMeetingDate(),mTimetTxtField.getText().toString());	
				}
				
			}
		});
		mdatePicker.setBounds(518, 102, 112, 27);
		add(mdatePicker);
		
		JLabel lblEventDate = new JLabel("Event Date");
		lblEventDate.setFont(new Font("Tahoma", Font.BOLD, 12));
		lblEventDate.setForeground(Color.WHITE);
		lblEventDate.setBounds(10, 102, 86, 27);
		add(lblEventDate);
		
		SqlDateModel model1 = new SqlDateModel();
		JDatePanelImpl datePanel1 = new JDatePanelImpl(model1);
		edatePicker = new JDatePickerImpl(datePanel1);

		edatePicker.setBounds(167, 102, 112, 27);
		add(edatePicker);
		
		cIDcomboBox = new JComboBox<String>();
		cIDcomboBox.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				synchronized (syncObject) {
					if(isChangeAllowed())
						filterList(cIDcomboBox.getSelectedItem().toString(),"CUSTOMER");
				}
			}
		});;
		AutoCompletion.enable(this.cIDcomboBox);
		
		cIDcomboBox.setBounds(167, 187, 137, 27);
		add(cIDcomboBox);
		
		lblGroomBride = new JLabel("Groom & Bride Names");
		lblGroomBride.setForeground(Color.WHITE);
		lblGroomBride.setFont(new Font("Tahoma", Font.BOLD, 12));
		lblGroomBride.setBounds(10, 287, 147, 14);
		add(lblGroomBride);
		
		
		gbNamescomboBox.setBounds(167, 281, 137, 27);
		add(gbNamescomboBox);
		
		lblPlace = new JLabel("Place");
		lblPlace.setForeground(Color.WHITE);
		lblPlace.setFont(new Font("Tahoma", Font.BOLD, 12));
		lblPlace.setBounds(10, 361, 126, 27);
		add(lblPlace);
		
		
		PlacecomboBox.setBounds(167, 362, 137, 27);
		add(PlacecomboBox);
		
	
		btnRefresh.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				synchronized (syncObject) {
					if(isChangeAllowed()){
						PlacecomboBox.setText("");
						mTimetTxtField.setText("hh:mm:ss");
						filterList(null,"ALL");
					}
				}
			}
		});
		btnRefresh.setIcon(new ImageIcon(CoordinatingMeeting.class.getResource("/resources/arrow-refresh-2-icon.png")));
		btnRefresh.setFont(new Font("Tahoma", Font.BOLD | Font.ITALIC, 12));
		btnRefresh.setBounds(247, 445, 137, 27);
		add(btnRefresh);
		
		iconlabel = new JLabel("");
		iconlabel.setIcon(LOGO);
		iconlabel.setBounds(0, 0, 786, 500);
		add(iconlabel);
	}

	public void allMeetingList(ArrayList<Meeting> list){
		m_allData = list;
		m_filterallallData = new ArrayList<>();
		System.out.println("size: "+list.size());
		syncObject= new Object();
		filterList(null,"ALL");
		
		
	}
	public void allManagers(ArrayList<String> mlist){
		all_managets = mlist;
	}
	private void filterList(String selectedItem, String scope)
	{
		
		int cFlag=0;
		synchronized (syncObject) {
			m_filterallallData.clear();
			
			switch(scope)
			{
			case "G_B_NAME":System.out.println("in lname");
				for(Meeting m : m_allData)
				{
					if(selectedItem.equals(m.getgName()+" & "+m.getbName()))
					{	
						m_filterallallData.add(m);
					}
				}
				break;
			case "CUSTOMER":
				for(Meeting m : m_allData)
				{
					if(selectedItem.equals(String.valueOf(m.getcID()))&&cFlag==0)
					{
						cFlag=1;
						System.out.println("in cust id filte");
						m_filterallallData.add(m);
					}
				}
				break;
			case "ALL":System.out.println("in all");
			m_filterallallData.addAll(m_allData);
				break;
			}
		}
		m_isChangeAllowed = false;
		filterComboBox(gbNamescomboBox,"G_B_NAME");
		filterComboBox(cIDcomboBox,"CUSTOMER");
		
		m_isChangeAllowed = true;

	}
	
	private void filterComboBox(JComboBox<String> combo, String scope)
	{
		combo.removeAllItems();
		System.out.println("m_filterallallData SIZE: "+m_filterallallData.size());
		synchronized (syncObject) {
			
			for(Meeting m : m_filterallallData)
			{
				switch(scope)
				{
				case "G_B_NAME":
					combo.addItem(m.getgName()+" & "+m.getbName());

					break;
				case "CUSTOMER":
						combo.addItem(String.valueOf(m.getcID()));
					break;
				}
			}
		}
	}
	private void filtermanager(String selectedItem1,String selectedItem2)
	{
		m_isChangeAllowed = false;
		ManagerInchargecomboBox.removeAllItems();
		for (Meeting m :m_allData) 
		{System.out.println("m.getmDate() "+m.getmDate());
		System.out.println("selectedItem1 "+selectedItem1);
		System.out.println("m.getmTime()"+m.getmTime());
		System.out.println("selectedItem2"+selectedItem2);
			
			if((m.getmDate().equals(selectedItem1) && m.getmTime().equals(selectedItem2)))
			{
				filterall_managets= m.getManagerIncharge();
			}	
		}
	
		for (String s :all_managets) 
		{
			if(!(s.equals(filterall_managets)))
			{
				ManagerInchargecomboBox.addItem(s);
			}	
		}	
		
		m_isChangeAllowed = true;

	}
	/**
	 * @return the m_isChangeAllowed
	 */
	public boolean isChangeAllowed(){
		return m_isChangeAllowed;
	}

	/**
	 * @return the event date in SQL format
	 */
	public String getEventDate()
	{
		java.sql.Date selectedDate = (java.sql.Date)edatePicker.getModel().getValue();
		if(selectedDate == null) return "";
		return selectedDate.toString();
	}
	/**
	 * @return the meeting date in SQL format
	 */
	public String getMeetingDate()
	{
		java.sql.Date selectedDate = (java.sql.Date)mdatePicker.getModel().getValue();
		if(selectedDate == null) return "";
		return selectedDate.toString();
	}

	/**
	 * @return the mTimetTxtField
	 */
	public JTextField getmTimetTxtField() {
		return mTimetTxtField;
	}

	/**
	 * @param mTimetTxtField the mTimetTxtField to set
	 */
	public void setmTimetTxtField(JTextField mTimetTxtField) {
		this.mTimetTxtField = mTimetTxtField;
	}

	/**
	 * @return the btnBack
	 */
	public JButton getBtnBack() {
		return btnBack;
	}

	/**
	 * @return the btnCoordinate
	 */
	public JButton getBtnCoordinate() {
		return btnCoordinate;
	}
	

	/**
	 * @return the managerInchargecomboBox
	 */
	public JComboBox<String> getManagerInchargecomboBox() {
		return ManagerInchargecomboBox;
	}

	/**
	 * @param managerInchargecomboBox the managerInchargecomboBox to set
	 */
	public void setManagerInchargecomboBox(JComboBox<String> managerInchargecomboBox) {
		ManagerInchargecomboBox = managerInchargecomboBox;
	}

	/**
	 * @param btnBack the btnBack to set
	 */
	public void setBtnBack(JButton btnBack) {
		this.btnBack = btnBack;
	}

	/**
	 * @param btnCoordinate the btnCoordinate to set
	 */
	public void setBtnCoordinate(JButton btnCoordinate) {
		this.btnCoordinate = btnCoordinate;
	}

	/**
	 * @return the mdatePicker
	 */
	public JDatePickerImpl getMdatePicker() {
		return mdatePicker;
	}

	/**
	 * @param mdatePicker the mdatePicker to set
	 */
	public void setMdatePicker(JDatePickerImpl mdatePicker) {
		this.mdatePicker = mdatePicker;
	}

	/**
	 * @return the edatePicker
	 */
	public JDatePickerImpl getEdatePicker() {
		return edatePicker;
	}

	/**
	 * @param edatePicker the edatePicker to set
	 */
	public void setEdatePicker(JDatePickerImpl edatePicker) {
		this.edatePicker = edatePicker;
	}

	/**
	 * @return the cIDcomboBox
	 */
	public JComboBox<String> getcIDcomboBox() {
		return cIDcomboBox;
	}

	/**
	 * @return the gbNamescomboBox
	 */
	public JComboBox<String> getGbNamescomboBox() {
		return gbNamescomboBox;
	}

	/**
	 * @param gbNamescomboBox the gbNamescomboBox to set
	 */
	public void setGbNamescomboBox(JComboBox<String> gbNamescomboBox) {
		this.gbNamescomboBox = gbNamescomboBox;
	}


	/**
	 * @param cIDcomboBox the cIDcomboBox to set
	 */
	public void setcIDcomboBox(JComboBox<String> cIDcomboBox) {
		this.cIDcomboBox = cIDcomboBox;
	}

	/**
	 * @return the placecomboBox
	 */
	public JTextField getPlacecomboBox() {
		return PlacecomboBox;
	}

	/**
	 * @param placecomboBox the placecomboBox to set
	 */
	public void setPlacecomboBox(JTextField placecomboBox) {
		PlacecomboBox = placecomboBox;
	}

	/**
	 * @return the btnRefresh
	 */
	public JButton getBtnRefresh() {
		return btnRefresh;
	}

	/**
	 * @param btnRefresh the btnRefresh to set
	 */
	public void setBtnRefresh(JButton btnRefresh) {
		this.btnRefresh = btnRefresh;
	}
}
